package com.kbt.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.fragment.*;
import com.kbt.model.data.BaseBean;
import com.kbt.model.data.CountBean;
import com.kbt.model.data.ShopCarBean;
import com.kbt.model.data.ShopCarListBean;
import com.kbt.net.RequestManager;
import com.kbt.ui.NoScrollViewPager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.CommonStatus;
import com.kbt.util.tool.TitleBarSetting;
import com.kbt.util.utils.*;
import com.readystatesoftware.viewbadger.BadgeView;

import java.util.*;

/**
 * 商品主页Activity
 * Created by kuajie on 15/7/30.
 */
public class StoreActivity extends FragmentActivity implements RequestManager.ResponseInterface{

    //定义Fragment页面存放数据
    private static List<Fragment> fragmentList;
    private static int currentIndex = 0;
    public static  UIHandler uiHandler;
    //声明ViewPager用于切换页面
    private NoScrollViewPager viewPager;
    //购物车数量
    private BadgeView badgeView;
    //适配器
    private StoreViewPagerAdapter adapter;
    private TextView app_btn;
    private ImageView app_btn_image;
    private TextView app_title;
    private RadioButton home;
    private RadioButton car;
    private RadioButton my;
    private RadioButton news;


    //购物车
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private ShopCarListBean shopCarListBean;
    private List<ShopCarBean> shopCarBeans = new ArrayList<ShopCarBean>();
    private TextView carCountView;
    private CountBean countBean;
    private BaseBean baseBean;
    private SharePreferenceUtils share;
    private  String phone;
    private  LoadingDialog mProgress;
    private static Boolean isExit = false;


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    /**
     * @description: Activity创建时调用
     * @param: savedInstanceState
     * @return: null
     * @author: zhangguokai
     * @date: 2015-07-31
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();

//        //是否更新
        if(BaseApplication.getInstance().isUpdate) {
            httpVersion();
        }

        //使用自定义标题栏
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.index_nagivation);
//        加载自定义标题栏布局
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.application_title_layout);
        carCountView = (TextView)findViewById(R.id.carCountView);
        app_btn = (TextView) findViewById(R.id.app_btn);
        app_btn_image = (ImageView) findViewById(R.id.app_btn_image);
        app_title = (TextView) findViewById(R.id.app_title);
        //购物车中是否有数据
//        getData();
        //创建fragment
        initFragment();
        //获取viewPager
        viewPager = (NoScrollViewPager) findViewById(R.id.sotre_pager);
        //  viewPager.setUiHandler(uiHandler);
        //viewPager.setScanScroll(false);
        viewPager.setOffscreenPageLimit(4);//预加载
        adapter = new StoreViewPagerAdapter(getSupportFragmentManager());
        //设置数据容器与触发事件
        viewPager.setAdapter(adapter);
        viewPager.setOnTouchListener(new ViewPagerOnTouchListener());
        //viewPager.dis
        //获取底部导舤栏按钮
        home = (RadioButton) findViewById(R.id.home_radio);
        car = (RadioButton) findViewById(R.id.car_radio);
        my = (RadioButton) findViewById(R.id.my_radio);
        news = (RadioButton) findViewById(R.id.news_radio);
        //为底部导航栏增加事件监听
        NavigationBarOnclickListener listener = new NavigationBarOnclickListener();
        home.setOnClickListener(listener);
        car.setOnClickListener(listener);
        my.setOnClickListener(listener);
        news.setOnClickListener(listener);

    }
    //是否更新
    private void httpVersion(){
        showProgress();
        if(Utils.isNetworkAvailable(StoreActivity.this)){
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectVersionUrl, Constants.RequestTag.selectVersionTag, map, BaseBean.class);
        }else{
            stopProgress();
            Toast.makeText(StoreActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    //获取购物车数据
    private void getData(){
        showProgress();
        if(Utils.isNetworkAvailable(StoreActivity.this)){
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectShopCarURL + phone+"&methodType=queryGoodsList", Constants.RequestTag.selectShopCarTag, map, ShopCarListBean.class);
        }else{
            stopProgress();
            Toast.makeText(StoreActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    //获取商品数量
    private void getShopType(){
        showProgress();
        if(Utils.isNetworkAvailable(StoreActivity.this)){
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectShopCarZhongLeiURL + phone+"&methodType=queryGoodsSum", Constants.RequestTag.selectShopCarZhongLeiTag, map, CountBean.class);
        }else{
            stopProgress();
            Toast.makeText(StoreActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    /***
     * @return void
     * @param:null
     * @description: 在Activity启动时调用，用于给页面按钮绑定事件等
     * @author zhanggk
     * @date 2015-07-31
     */
    @Override
    public void onStart() {
        //重写onStart方法时必须加入此方法
        super.onStart();
        BaseApplication.getInstance().addActivity(StoreActivity.this);
        share = new SharePreferenceUtils(StoreActivity.this);
        phone = share.getString("user_mobile","");
        uiHandler = new UIHandler();
        BaseApplication.getInstance().activity = StoreActivity.this;
        //购物车中是否有数据
        getData();

    }

    private void  init(){
        //获取购物车商品种类
        if(share.getString("user_mobile","")!=null&&!"".equals(share.getString("user_mobile",""))) {
            getShopType();
        } else{
            Message msg = new Message();
            msg.what = StaticData.SHOPCAR_QUAN_FLAG;
            msg.obj = 0;
            uiHandler.sendMessage(msg);
            BaseApplication.getInstance().shopType = 0;
        }

//        Intent intent = getIntent();
//        Bundle bl = intent.getBundleExtra("bundle");
        int index = StaticData.INDEX_HOME;
        //int viewIndex = StaticData.HOME_FRAGMENT_INDEX;
//        if (bl != null) {
//            index = bl.getSerializable("currentItem") ==
//                    null ? StaticData.INDEX_HOME : Integer.parseInt(bl.getSerializable("currentItem").toString());
        index = BaseApplication.getInstance().currentItem ==
                null ? StaticData.INDEX_HOME : Integer.parseInt(BaseApplication.getInstance().currentItem.toString());
        String carStatus = CommonStatus.carStatus == null ? "EMPTY" : String.valueOf(CommonStatus.carStatus);
        if (index == StaticData.INDEX_HOME) {
            currentIndex = StaticData.HOME_FRAGMENT_INDEX;
        } else if (index == StaticData.INDEX_CAR) {
            currentIndex = StaticData.CAR_FRAGMENT_INDEX;
            changCarFragment(carStatus);
        }else if(index == StaticData.INDEX_NEWS){
            currentIndex = StaticData.NEWS_FRAGMENT_INDEX;
        } else {
            currentIndex = StaticData.MY_FRAGMENT_INDEX;
        }

//        }

        // findViewById(index).callOnClick();
        changeView(currentIndex);
//        ((RadioButton) findViewById(index)).setChecked(true);

//        //底部选中
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                switch (arg0) {
                    case 0:
                        home.setChecked(true);
                        BaseApplication.getInstance().currentItem = StaticData.INDEX_HOME;
                        break;
                    case 1:
                        car.setChecked(true);
                        BaseApplication.getInstance().currentItem = StaticData.INDEX_CAR;
                        break;
                    case 2:
                        my.setChecked(true);
                        BaseApplication.getInstance().currentItem = StaticData.INDEX_MY;
                        break;
                    case 3:
                        news.setChecked(true);
                        BaseApplication.getInstance().currentItem = StaticData.INDEX_NEWS;
                        break;
                    default:
                        break;
                }
            }
            @Override
            public void onPageSelected(int id) {

            }

        });
    }



    /**
     * @description: 改变当前视图
     * @param: index当前视图位置
     */

    public void changeView(int index) {
        currentIndex = index;
        changeTitle(index);
        if (index == StaticData.CAR_FRAGMENT_INDEX) {
            changCarFragment(CommonStatus.carStatus);
        }
        viewPager.setCurrentItem(index, true);
    }

    /***
     * 初始化页面
     */
    private void initFragment() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(0, new HomeFragment());
        //购物车的状态变换
        if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_EMPTY)) {
            fragmentList.add(1, new GoodCarEmptyFragment());
        } else if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_COMPLETE)) {

            fragmentList.add(1, new GoodCarCompleteFragment());
        } else if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_EDIT)) {

            fragmentList.add(1, new GoodCarEditFragment());
        } else {
            fragmentList.add(1, new GoodCarEmptyFragment());

        }

        fragmentList.add(2, new MyFragment());
        fragmentList.add(3, new NewsFragment());
    }

    public void changeFragment(int index, Fragment fragment) {
        fragmentList.remove(index);
        fragmentList.add(index, fragment);
    }

    /****
     * 更新标题栏信息
     *
     * @param index
     */
    private void changeTitle(int index) {
        Message msg = new Message();
        switch (index) {
            case StaticData.HOME_FRAGMENT_INDEX:
                msg.what = StaticData.UPDATE_HOME_TITLE_FLAG;
                break;
            case StaticData.CAR_FRAGMENT_INDEX:
                if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_COMPLETE)) {
                    msg.what = StaticData.UPDATE_CAR_COMP_TITLE_FLAG;
                } else if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_EMPTY)) {
                    msg.what = StaticData.UPDATE_CAR_EMPTY_TITLE_FLAG;
                } else if (CommonStatus.carStatus.equals(StaticData.GOOD_CAR_EDIT)) {
                    msg.what = StaticData.UPDATE_CAR_EDIT_TITLE_FLAG;
                }
                break;
            case StaticData.MY_FRAGMENT_INDEX:
                msg.what = StaticData.UPDATE_MY_INFO_TITLE_FLAG;
                break;
            case StaticData.NEWS_FRAGMENT_INDEX:
                msg.what = StaticData.UPDATE_NEWS_INFO_TITLE_FLAG;
                break;
        }
        uiHandler.sendMessage(msg);
    }

    /********
     * 更新标题栏按钮事件
     *
     * @param app_btn
     * @param what
     */
    private void setTitleBtnClickListener(View app_btn, int what) {
        switch (what) {
            case StaticData.UPDATE_CAR_COMP_TITLE_FLAG:
                //切换购物车的Fragment
                app_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommonStatus.carStatus = StaticData.GOOD_CAR_EDIT;
                        changeView(StaticData.CAR_FRAGMENT_INDEX);
                    }
                });

                break;
            case StaticData.UPDATE_CAR_EDIT_TITLE_FLAG:
                //切换购物车的Fragement
                app_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommonStatus.carStatus = StaticData.GOOD_CAR_COMPLETE;
                        changeView(StaticData.CAR_FRAGMENT_INDEX);
                    }
                });
                break;
            case StaticData.UPDATE_CAR_EMPTY_TITLE_FLAG:
                app_btn.setOnClickListener(null);
                break;
            case StaticData.UPDATE_HOME_TITLE_FLAG:
                app_btn.setOnClickListener(null);
                break;
            case StaticData.UPDATE_MY_INFO_TITLE_FLAG:
                //打开配置页面
                app_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(StoreActivity.this, SettingActivity.class);
                        startActivityForResult(intent, StaticData.STORE_FOR_SETTING);
                    }
                });
                break;
            case StaticData.UPDATE_NEWS_INFO_TITLE_FLAG:
                app_btn.setOnClickListener(null);
                break;
            default:
                app_btn.setOnClickListener(null);
                break;
        }
    }

    /****
     * 切换购物车状态
     *
     * @param carStatus
     */
    private void changCarFragment(String carStatus) {
        CommonStatus.carStatus = carStatus;
        if (carStatus.length() > 0) {
            Fragment fragment = fragmentList.get(StaticData.CAR_FRAGMENT_INDEX);
            if (carStatus.equals(StaticData.GOOD_CAR_EMPTY) && !(fragment instanceof GoodCarEmptyFragment)) {
                changeFragment(StaticData.CAR_FRAGMENT_INDEX, new GoodCarEmptyFragment());
                adapter.notifyDataSetChanged();

            } else if (carStatus.equals(StaticData.GOOD_CAR_COMPLETE) && !(fragment instanceof GoodCarCompleteFragment)) {
                changeFragment(StaticData.CAR_FRAGMENT_INDEX, new GoodCarCompleteFragment());
                adapter.notifyDataSetChanged();

            } else if (carStatus.equals(StaticData.GOOD_CAR_EDIT) && !(fragment instanceof GoodCarEditFragment)) {
                changeFragment(StaticData.CAR_FRAGMENT_INDEX, new GoodCarEditFragment());
                adapter.notifyDataSetChanged();

            }
        }
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        stopProgress();
        if (tag == Constants.RequestTag.selectShopCarTag) {
            if (parsedGSON != null) {
                shopCarListBean = (ShopCarListBean) parsedGSON;
                // 判断是否是有数据
                if (shopCarListBean.getResult().equals("success")) {// 成功
                    if (shopCarListBean.getData().size() > 0) {//获取到数据
                        shopCarBeans =shopCarListBean.getData();
                        CommonStatus.carStatus = "COMPLETE";
                    } else {
                        CommonStatus.carStatus = "EMPTY";
                    }
                    init();
                }
            }
        }
        if (tag == Constants.RequestTag.selectShopCarZhongLeiTag) {
            if (parsedGSON != null) {
                countBean = (CountBean) parsedGSON;
                // 判断是否是有数据
                if (countBean.getResult().equals("success")) {// 成功
                        Message msg = new Message();
                        msg.what = StaticData.SHOPCAR_QUAN_FLAG;
                        msg.obj = countBean.getData();
                        uiHandler.sendMessage(msg);
                        BaseApplication.getInstance().shopType = countBean.getData();
                }
            }
        }
        if (tag == Constants.RequestTag.selectVersionTag) {
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                // 判断是否是有数据
                if (baseBean.getResult().equals("success")) {// 成功
                    int versionCode = getVersionCode(StoreActivity.this);
                    if(versionCode<Integer.parseInt(baseBean.getMsg())) {
                            UpdateManager updatemanager = new UpdateManager(StoreActivity.this);
                            updatemanager.showNoticeDialog();
                         }
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        stopProgress();
        Toast.makeText(StoreActivity.this, R.string.noWangluo, Toast.LENGTH_SHORT).show();
    }

    /**
     * @description: 点击事件监听器，主要用于处理按钮、控件的单击事件，此处用于处理底部导舤切换面，页面的切换
     * @type: InnerClass
     * @author: zhguokai
     * @date: 2015-07-31
     */
    class NavigationBarOnclickListener implements View.OnClickListener {

        /**
         * @description: 实现onClick事件，用于处理点击时操作
         * @type： event method
         * @author: zhguokai
         * @date: 2015-07-31
         ***/
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.home_radio:
                    if (btnUtil.isFastDoubleClick()) {
                        return;
                    }
//                    HomeFragment homeFragment = new HomeFragment();
//                    changeFragment(0, homeFragment);
                    adapter.notifyDataSetChanged();
                    changeView(0);
                    break;
                case R.id.car_radio:
                    changeView(1);
                    break;
                case R.id.my_radio:
                    changeView(2);
                    break;
                case R.id.news_radio:
                    changeView(3);
                    break;
                default:
                    break;

            }

        }
    }



    /**
     * @description: 触摸事件监听器，页面触动，滑动事件
     * @type: InnerClass
     * @author: zhguokai
     * @date: 2015-07-31
     */
    class ViewPagerOnTouchListener implements View.OnTouchListener {

        /**
         * @description:触摸时触发的事件
         * @param: view viewPager视图
         * @param: motionEvent 移动事件
         * @return:true 返回True终止事件
         */


        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return true;
        }

    }

    /**
     * @description: viewPager的适配器类，为viewPager提供视图数据
     * @type: InnerClass
     * @author: zhguokai
     * @date: 2015-07-31
     */

    class StoreViewPagerAdapter extends FragmentStatePagerAdapter {
        //适配器管理工具
        private FragmentManager manger = null;

        //构造函数
        public StoreViewPagerAdapter(FragmentManager fm) {
            super(fm);
            this.manger = fm;
        }

        //获取当前Fragment
        @Override
        public Fragment getItem(int index) {
            Fragment fragment = fragmentList.get(index);
            return fragment;
        }

        //获取Fragment的数量
        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }
    }

    /*********
     * 更新标题栏
     */
    private class UIHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case StaticData.UPDATE_CAR_COMP_TITLE_FLAG:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.VISIBLE);
                    TitleBarSetting.setTitleBtn(app_btn, "编辑", Color.TRANSPARENT);
                    TitleBarSetting.setTitleText(app_title, StaticData.SHOOPING_CAR_TITLE);
                    setTitleBtnClickListener(app_btn, msg.what);
                    break;
                case StaticData.UPDATE_CAR_EDIT_TITLE_FLAG:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.VISIBLE);
                    TitleBarSetting.setTitleBtn(app_btn, "完成", Color.TRANSPARENT);
                    TitleBarSetting.setTitleText(app_title, StaticData.SHOOPING_CAR_TITLE);
                    setTitleBtnClickListener(app_btn, msg.what);
                    break;
                case StaticData.UPDATE_CAR_EMPTY_TITLE_FLAG:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.GONE);
                    TitleBarSetting.setTitleBtn(app_btn, "", Color.TRANSPARENT);
                    TitleBarSetting.setTitleText(app_title, StaticData.SHOOPING_CAR_TITLE);
                    setTitleBtnClickListener(app_btn, msg.what);
                    break;
                case StaticData.UPDATE_HOME_TITLE_FLAG:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.GONE);
                    TitleBarSetting.setTitleBtn(app_btn, "", Color.TRANSPARENT);
                    TitleBarSetting.setTitleText(app_title, StaticData.APPLICATION_TITLE);
                    setTitleBtnClickListener(app_btn, msg.what);
                    break;

                case StaticData.UPDATE_MY_INFO_TITLE_FLAG:
                    app_btn_image.setVisibility(View.VISIBLE);
                    app_btn.setVisibility(View.GONE);
                    TitleBarSetting.setTitleText(app_title, StaticData.MY_INFO_TITLE);
                    setTitleBtnClickListener(app_btn_image, msg.what);
                    break;
                case StaticData.UPDATE_NEWS_INFO_TITLE_FLAG:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.GONE);
                    TitleBarSetting.setTitleBtn(app_btn, "", Color.TRANSPARENT);
                    TitleBarSetting.setTitleText(app_title, StaticData.NEWS_INFO_TITLE);
//                    setTitleBtnClickListener(app_btn, msg.what);
                    break;
                case StaticData.SHOPCAR_QUAN_FLAG:
                    if(badgeView!=null){
                        if(msg.obj.toString().equals("0")){
                            badgeView.hide();
                        }else {
                            badgeView.setText(msg.obj.toString()+"");
                            badgeView.hide();
                            badgeView.show();
                        }
                    }else{
                        badgeView = new BadgeView(StoreActivity.this,carCountView);
                        if(!msg.obj.toString().equals("0")) {
                            badgeView.setText(msg.obj.toString()+"");
                            badgeView.show();
                        }else{
                            badgeView.hide();
                        }
                    }
                    break;
                default:
                    app_btn_image.setVisibility(View.GONE);
                    app_btn.setVisibility(View.GONE);
                    setTitleBtnClickListener(app_btn, msg.what);
                    break;

            }

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//        super.onSaveInstanceState(outState, outPersistentState);
    }
    /**
     * progressDialog的展示
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new LoadingDialog(StoreActivity.this,"");
            mProgress.setCancelable(true);
            mProgress.show();

        } else {
            if (mProgress.isShowing()) {
                stopProgress();
            } else {
                mProgress.show();
            }
        }
    }


    /**
     * progressDialog的消失
     */
    public void stopProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }
    /**
     * 获取软件版本号
     *
     * @param context
     * @return
     */
    private int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            // 获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = context.getPackageManager().getPackageInfo("com.kbt.activity", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public static void jianCarNumber(){
        Message msg = new Message();
        msg.what = StaticData.SHOPCAR_QUAN_FLAG;
        msg.obj = BaseApplication.getInstance().shopType;
        uiHandler.sendMessage(msg);
    }


    //双击退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return false;
    }

    //双击退出
    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            // 准备退出
            isExit = true;
            Toast.makeText(StoreActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    // 取消退出
                    isExit = false;
                }
                // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
            }, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }



}
